package cn.miju.admin.system.controller;

import cn.hutool.extra.servlet.ServletUtil;
import cn.miju.admin.framework.annotation.NoToken;
import cn.miju.admin.system.service.LoginService;
import cn.miju.admin.system.vo.AccountLoginInVO;
import cn.miju.admin.system.vo.LoginOutVO;
import cn.miju.admin.system.vo.RegisterInVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <pre>
 * 登录注册
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-18 15:18
 */
@RestController
@Api(tags = "系统-登录注册")
@ApiSort(1)
@RequestMapping("/system")
public class LoginController {

    private final LoginService service;

    public LoginController(LoginService service) {
        this.service = service;
    }

    /**
     * 账号登录
     * @param inVO {@link AccountLoginInVO}
     * @return {@link LoginOutVO}
     */
    @NoToken
    @ApiOperation("账号登录")
    @ApiOperationSupport(order = 1, author = "zhuming")
    @PostMapping(value = "/accountLogin")
    public LoginOutVO accountLogin(@RequestBody @Validated AccountLoginInVO inVO, HttpServletRequest request) {
        return service.login(inVO, ServletUtil.getClientIP(request));
    }
    /**
     * 用户注册
     * @param inVO {@link RegisterInVO}
     */
    @NoToken
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 2, author = "zhuming")
    @PostMapping(value = "/register")
    public String register(@RequestBody @Validated RegisterInVO inVO, HttpServletRequest request) {
        service.register(inVO, ServletUtil.getClientIP(request));
        return "注册成功！";
    }
}
